home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / clipart / 256scan.zip / 256SCAN.CPP next >
C/C++ Source or Header  |  1993-03-23  |  9KB  |  321 lines

  1. // ---- 256 color converter from 256 b&w gray scans -----------256scan.cpp
  2.  
  3. // -------------- Include files -------------------------------------
  4.  
  5. #include "stdio.h"
  6. #include "alloc.h"
  7. #include "graphics.h"
  8. #include "dos.h"
  9. #include "stdlib.h"
  10. #include "mem.h"
  11. #include "conio.h"
  12. #include "io.h"
  13.  
  14. // ----------- Prototypes --------------------------------------
  15.  
  16. void zRPCXRead(void);          // read, decode R.PCX & dump as R.BIN
  17. void zGPCXRead(void);          // read, decode G.PCX & dump as G.BIN
  18. void zBPCXRead(void);          // read, decode B.PCX & dump as B.BIN
  19. void zRGBBMPMake(void);        // read, combine and dump as RGB.BMP
  20.  
  21. // ----------- Typedefs and Structs -----------------------------------
  22.  
  23. typedef struct       // define 8-bit header
  24.  {
  25.  char manufacturer;
  26.  char version;
  27.  char encoding;
  28.  char bits_per_pixel;
  29.  int xmin,ymin;
  30.  int xmax,ymax;
  31.  int hres;
  32.  int vres;
  33.  char palette[48];
  34.  char reserved;
  35.  char color_planes;
  36.  int bytes_per_line;
  37.  int palette_type;
  38.  char filler[58];
  39.  } PCXHEAD;
  40.  
  41.  PCXHEAD Header;   // where the header lives
  42.  
  43. // ------------------ Global Variables ---------------------------------
  44.  
  45. char palette[768];           // where the palette lives
  46. int width,depth;
  47. int bytes,bits;
  48. int b=0;
  49. unsigned long Length = 0;
  50.  
  51. // ---------------------- Main Executive Program ---------------------
  52. main()
  53.   {
  54.   printf("256 Gray-Scale to Color Conversion.\n");
  55.   printf("Copyright 1993 by Mark Taylor, Highland, Illinois.\n");
  56.   printf("\n");
  57.   printf("Beginning conversion. \n");
  58.   printf("Converting and dumping R.PCX. \n");
  59.   zRPCXRead();              // Read R.PCX file
  60.   printf("Converting and dumping G.PCX. \n");
  61.   zGPCXRead();              // Read G.PCX file
  62.   printf("Converting and dumping B.PCX. \n");
  63.   zBPCXRead();              // Read B.PCX file
  64.   printf("Creating 24 bit RGB.BMP output file. \n");
  65.   zRGBBMPMake();            // Combine BIN files and make BMP file
  66.   printf("Conversion completed. \n");
  67.   exit(0);
  68.   }
  69.  
  70. // ------ Read R.PCX ---------------------------------------------
  71. void zRPCXRead(void)
  72. {
  73. FILE *fp;               // R.PCX
  74. FILE *fp1;              // R.BIN
  75. char *p;
  76.  
  77. // attempt to open R.PCX
  78. if((fp=fopen("R.PCX","rb"))==NULL)
  79.   {fprintf(stderr,"Can not find R.PCX.  Check subdirectory. \n");exit(1);}
  80.  
  81. // read in header
  82. if(fread((char*)&Header,1,sizeof(PCXHEAD),fp) != sizeof(PCXHEAD))
  83.   {fprintf(stderr,"Error reading the R.PCX header. \n");exit(1);}
  84.  
  85. // check that it's a picture
  86. if(Header.manufacturer != 0x0a || Header.version != 5)
  87.   {printf("The R.PCX is not a 256 color PCX file. \n");exit(1);}
  88. if(Header.bits_per_pixel == 1) bits = Header.color_planes;
  89.   else bits = Header.bits_per_pixel;
  90. if(bits != 8)
  91.   {fprintf(stderr,"The R.PCX has the wrong number of colors. \n");exit(1);}
  92.  
  93. // perform some housekeeping
  94. width = (Header.xmax - Header.xmin) + 1;
  95. depth = (Header.ymax - Header.ymin) + 1;
  96. bytes = Header.bytes_per_line;
  97.  
  98. fp1=fopen("R.BIN","ab");
  99.  
  100. int i=0;
  101. for(i=0; i<depth; i++)
  102.   {
  103.   int n=0,b,c;
  104.   if ((p=(char *)malloc(bytes)) == NULL) {
  105.     printf("Not enough memory for buffer.\n");exit(1);}
  106.   {do
  107.     { c=fgetc(fp) & 0xff;      // get a key byte
  108.       if((c & 0xc0) == 0xc0)     // if it's a run of bytes
  109.       { b=c & 0x3f;             // off the high bits
  110.         c=fgetc(fp);          // get the run byte
  111.         while(b--) {p[n++]=~c; fputc(c,fp1);}  // run the byte
  112.        }
  113.       else {fputc(c,fp1); p[n++]=~c;}
  114.     }
  115.   while (n < bytes);
  116.   }
  117.   free(p);
  118.   }
  119. fclose(fp);
  120. fclose(fp1);
  121. return;
  122. }
  123.  
  124.  
  125. // ------ Read G.PCX ---------------------------------------------
  126. void zGPCXRead(void)
  127. {
  128. FILE *fp;               // R.PCX
  129. FILE *fp1;              // R.BIN
  130. char *p;
  131.  
  132. // attempt to open G.PCX
  133. if((fp=fopen("G.PCX","rb"))==NULL)
  134.   {fprintf(stderr,"Can not find G.PCX.  Check subdirectory. \n");exit(1);}
  135.  
  136. // read in header
  137. if(fread((char*)&Header,1,sizeof(PCXHEAD),fp) != sizeof(PCXHEAD))
  138.   {fprintf(stderr,"Error reading the G.PCX header. \n");exit(1);}
  139.  
  140. // check that it's a picture
  141. if(Header.manufacturer != 0x0a || Header.version != 5)
  142.   {printf("The G.PCX is not a 256 color PCX file. \n");exit(1);}
  143. if(Header.bits_per_pixel == 1) bits = Header.color_planes;
  144.   else bits = Header.bits_per_pixel;
  145. if(bits != 8)
  146.   {fprintf(stderr,"The G.PCX has the wrong number of colors. \n");exit(1);}
  147.  
  148. // perform some housekeeping
  149. width = (Header.xmax - Header.xmin) + 1;
  150. depth = (Header.ymax - Header.ymin) + 1;
  151. bytes = Header.bytes_per_line;
  152.  
  153. fp1=fopen("G.BIN","ab");
  154.  
  155. int i=0;
  156. for(i=0; i<depth; i++)
  157.   {
  158.   int n=0,b,c;
  159.   if ((p=(char *)malloc(bytes)) == NULL) {
  160.     printf("Not enough memory for buffer.\n");exit(1);}
  161.   {do
  162.     { c=fgetc(fp) & 0xff;      // get a key byte
  163.       if((c & 0xc0) == 0xc0)     // if it's a run of bytes
  164.       { b=c & 0x3f;             // off the high bits
  165.         c=fgetc(fp);          // get the run byte
  166.         while(b--) {p[n++]=~c; fputc(c,fp1);}  // run the byte
  167.        }
  168.       else {fputc(c,fp1); p[n++]=~c;}
  169.     }
  170.   while (n < bytes);
  171.   }
  172.   free(p);
  173.   }
  174. fclose(fp);
  175. fclose(fp1);
  176. return;
  177. }
  178.  
  179. // ------ Read B.PCX ---------------------------------------------
  180. void zBPCXRead(void)
  181. {
  182. FILE *fp;               // R.PCX
  183. FILE *fp1;              // R.BIN
  184. char *p;
  185.  
  186. // attempt to open B.PCX
  187. if((fp=fopen("B.PCX","rb"))==NULL)
  188.   {fprintf(stderr,"Can not find B.PCX.  Check subdirectory. \n");exit(1);}
  189.  
  190. // read in header
  191. if(fread((char*)&Header,1,sizeof(PCXHEAD),fp) != sizeof(PCXHEAD))
  192.   {fprintf(stderr,"Error reading the B.PCX header. \n");exit(1);}
  193.  
  194. // check that it's a picture
  195. if(Header.manufacturer != 0x0a || Header.version != 5)
  196.   {printf("The B.PCX is not a 256 color PCX file. \n");exit(1);}
  197. if(Header.bits_per_pixel == 1) bits = Header.color_planes;
  198.   else bits = Header.bits_per_pixel;
  199. if(bits != 8)
  200.   {fprintf(stderr,"The B.PCX has the wrong number of colors. \n");exit(1);}
  201.  
  202. // perform some housekeeping
  203. width = (Header.xmax - Header.xmin) + 1;
  204. depth = (Header.ymax - Header.ymin) + 1;
  205. bytes = Header.bytes_per_line;
  206.  
  207. fp1=fopen("B.BIN","ab");
  208.  
  209. int i=0;
  210. for(i=0; i<depth; i++)
  211.   {
  212.   int n=0,b,c;
  213.   if ((p=(char *)malloc(bytes)) == NULL) {
  214.     printf("Not enough memory for buffer.\n");exit(1);}
  215.  
  216.   {do
  217.     { c=fgetc(fp) & 0xff;      // get a key byte
  218.       if((c & 0xc0) == 0xc0)     // if it's a run of bytes
  219.       { b=c & 0x3f;             // off the high bits
  220.         c=fgetc(fp);          // get the run byte
  221.         while(b--) {p[n++]=~c; fputc(c,fp1);}  // run the byte
  222.        }
  223.       else {fputc(c,fp1); p[n++]=~c;}
  224.     }
  225.   while (n < bytes);
  226.   }
  227.   free(p);
  228.   }
  229. fclose(fp);
  230. fclose(fp1);
  231.  
  232. int Lwidth=width;
  233. int Ldepth=depth;
  234. ((unsigned long)Length) = (((unsigned long)Lwidth) * ((unsigned long)Ldepth));
  235. return;
  236. }
  237.  
  238.  
  239. // ---- Combine R.BIN/G.BIN/B.BIN to RGB.BMP --------------------------
  240. void zRGBBMPMake(void)
  241. {
  242. FILE *fp1;        // R.BIN File
  243. FILE *fp2;        // G.BIN File
  244. FILE *fp3;        // B.BIN File
  245. FILE *fp4;        // RGB.BMP File
  246.  
  247. char b;          // byte
  248.  
  249. typedef struct {
  250.         char id[2];
  251.         long FileSize;
  252.         int Reserved[2];
  253.         long HeaderSize;
  254.         long InfoSize;
  255.         long BMPWidth;
  256.         long BMPDepth;
  257.         int biPlanes;
  258.         int Bits;
  259.         long biCompression;
  260.         long biSizeImage;
  261.         long biXPelsPerMeter;
  262.         long biYPelsPerMeter;
  263.         long biClrUsed;
  264.         long biClrImportant;
  265.         }BMPHEAD;
  266.  
  267. BMPHEAD bmp;
  268.  
  269. if((fp1=fopen("R.BIN","rb")) == NULL)
  270.   {fprintf(stderr,"Can not open R.BIN file. \n"); exit(1);}
  271.  
  272. if((fp2=fopen("G.BIN","rb")) == NULL)
  273.   {fprintf(stderr,"Can not open G.BIN file. \n"); exit(1);}
  274.  
  275. if((fp3=fopen("B.BIN","rb")) == NULL)
  276.   {fprintf(stderr,"Can not open B.BIN file. \n"); exit(1);}
  277.  
  278. if((fp4=fopen("RGB.BMP","ab")) == NULL)
  279.   {fprintf(stderr,"Can not open RGB.BMP file. \n"); exit(1);}
  280.  
  281. // write the header to RGB.BMP
  282. memset((char *)&bmp,0,sizeof(BMPHEAD));
  283. memcpy(bmp.id,"BM",2);
  284. bmp.InfoSize=0x28L;
  285. bmp.HeaderSize=54L;
  286. bmp.BMPWidth=(long)width;
  287. bmp.BMPDepth=(long)depth;
  288. bmp.biPlanes=1;
  289. bmp.Bits=24;
  290. bmp.biCompression=0L;
  291. bmp.FileSize=(long)((width*depth*3)+54L);
  292. bmp.biSizeImage=(long)(width*depth*3);
  293. bmp.biClrUsed=0L;
  294. bmp.biClrImportant=0L;
  295. bmp.biXPelsPerMeter=0L;
  296. bmp.biYPelsPerMeter=0L;
  297.  
  298. fwrite((char *)&bmp,1,sizeof(BMPHEAD),fp4);
  299.  
  300.  
  301.  
  302. unsigned long r=0;
  303. for(r=0; r<(Length+depth); ++r)
  304.   {
  305.    b=fgetc(fp1);
  306.    fputc(b,fp4);
  307.  
  308.    b=fgetc(fp2);
  309.    fputc(b,fp4);
  310.  
  311.    b=fgetc(fp3);
  312.    fputc(b,fp4);
  313.   }
  314.  
  315. fclose(fp1);
  316. fclose(fp2);
  317. fclose(fp3);
  318. fclose(fp4);
  319. return;
  320. }
  321.